/******************************************************************************
 * * setting_restore.c - implementation of restore setting functions
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "setting_global.h"

/**
 * sub-module entry
 * param (in) void
 * return  void
 */
VOID Setting_Restore_Main(VOID)
{
    // init manage variable
}

/**
 * sub-module event handler
 * param (in) void
 * return  void
 */
VOID Setting_Restore_Event_handle(VOID)
{
    // event and message dispatch
}

/**
 * Callback of checking security code confirm
 * param (in) Result: SETTING_RESULT
 * return VOID*:
 */
VOID Display_Setting_Security_Restore_Factory_ChkSecurityCodeCnf_Cb(SETTING_RESULT Result)
{
    NV_UI_Settings_t        *pNvUiSetting;
    Setting_Phone_Setting_t *PhoneSetting;

    if (SETTING_OK == Result)
    {
        // Restore all default settings to NV
        UI_NV_Reset_Default(NV_SECTION_UI_SETTINGS);

        // malloc memory for NVM reading buffer
        pNvUiSetting = Hal_Mem_Alloc(sizeof(NV_UI_Settings_t));

        // read NVM
        UI_NV_Read_Req(NV_SECTION_UI_SETTINGS, 0, sizeof(NV_UI_Settings_t), (UINT8 *)pNvUiSetting);

        // call sub module init functions
        Setting_Dualsim_Init_Req(pNvUiSetting);
        Setting_Tone_Init_Req(pNvUiSetting);
        Setting_Display_Init_Req(pNvUiSetting);
        Setting_Profile_Init_Req(pNvUiSetting);
        Setting_Time_Init_Req(pNvUiSetting);
        Setting_Call_Init_Req(pNvUiSetting);
        Setting_Phone_Init_Req(pNvUiSetting);
        Setting_Security_Init_Req(pNvUiSetting);
#if USE_LV_WLAN != 0
        hal_wlan_reset();
#endif

        // notify framework
        PhoneSetting             = (Setting_Phone_Setting_t *)Hal_Mem_Alloc(sizeof(Setting_Phone_Setting_t));
        PhoneSetting->ChangeMark = 0;
        if ((FALSE == g_SettingSimStatus[MMI_MODEM_SIM_1]) || (FALSE == g_SettingSimStatus[MMI_MODEM_SIM_2]))
        {
            if (FALSE == g_SettingSimStatus[MMI_MODEM_SIM_1])
            {
            }
            if (FALSE == g_SettingSimStatus[MMI_MODEM_SIM_2])
            {
            }
        }
        else
        {
            PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_DUAL_STAND_BY;
            if (SETTING_DEFAULT_DULT_SIM == pNvUiSetting->DualSimSetting.DefaultSim4Standby)
            {
                PhoneSetting->DualStandby = IF_SETTING_DUAL_SIM_STANDBY;
            }
            else if (SETTING_DEFAULT_SIM1 == pNvUiSetting->DualSimSetting.DefaultSim4Standby)
            {
                PhoneSetting->DualStandby = IF_SETTING_SIM_1_STANDBY;
            }
            else if (SETTING_DEFAULT_SIM2 == pNvUiSetting->DualSimSetting.DefaultSim4Standby)
            {
                PhoneSetting->DualStandby = IF_SETTING_SIM_2_STANDBY;
            }

            PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_NAME_OF_SIM1;
            Hal_Mem_Copy(PhoneSetting->NameOfSIM1, pNvUiSetting->DualSimSetting.SimName[0], NV_SETTING_MAX_SIM_NAME_LEN);

            PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_NAME_OF_SIM2;
            Hal_Mem_Copy(PhoneSetting->NameOfSIM2, pNvUiSetting->DualSimSetting.SimName[1], NV_SETTING_MAX_SIM_NAME_LEN);
        }

        PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_WALL_PAPER;
        PhoneSetting->WallPaper   = SettingWallPaterList[pNvUiSetting->DisplaySetting.CurrentWallPaperIndex].WallPaper;

        PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_TIME_DISPLAY;
        if (SETTING_TIME_HIDE == pNvUiSetting->TimeSetting.TimeDisplay)
        {
            PhoneSetting->TimeDisplay.TimeDisplay = IF_SETTING_TIME_HIDE;
        }
        else
        {
            PhoneSetting->TimeDisplay.TimeDisplay = IF_SETTING_TIME_SHOW;
        }
        if (SETTING_TIME_FORMAT_12H == pNvUiSetting->TimeSetting.TimeFormat)
        {
            PhoneSetting->TimeDisplay.TimeFormat = IF_SETTING_TIME_FORMAT_12H;
        }
        else
        {
            PhoneSetting->TimeDisplay.TimeFormat = IF_SETTING_TIME_FORMAT_24H;
        }

        PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_CURRENT_LANGUAGE;
        if (0 == pNvUiSetting->PhoneSetting.CurrentLanguage)
        {
            PhoneSetting->CurrentLanguage = SETTING_LANGUAGE_EN;
        }
        else if (1 == pNvUiSetting->PhoneSetting.CurrentLanguage)
        {
            PhoneSetting->CurrentLanguage = SETTING_LANGUAGE_CN_SIMPLE;
        }
        else if (2 == pNvUiSetting->PhoneSetting.CurrentLanguage)
        {
            PhoneSetting->CurrentLanguage = SETTING_LANGUAGE_CN_TRADITIONAL;
        }

        PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_AUTO_GUARD_ON;
        PhoneSetting->AutoGuardOn = pNvUiSetting->PhoneSetting.AutoGuardOn;

        PhoneSetting->ChangeMark    |= IF_SETTING_CHANGE_MARK_AUTO_GUARD_DELAY;
        PhoneSetting->AutoGuardDelay = pNvUiSetting->PhoneSetting.AutoGuardDelay;

        PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_KEYGUARD_ON;
        PhoneSetting->KeyguardOn  = pNvUiSetting->PhoneSetting.KeyguardOn;

        PhoneSetting->ChangeMark   |= IF_SETTING_CHANGE_MARK_STARTUP_TONE_ON;
        PhoneSetting->StartupToneOn = pNvUiSetting->PhoneSetting.StartupToneOn;

        PhoneSetting->ChangeMark  |= IF_SETTING_CHANGE_MARK_FLIGHT_MODE_ON;
        PhoneSetting->FlightModeOn = pNvUiSetting->PhoneSetting.FlightModeOn;

        PhoneSetting->ChangeMark   |= IF_SETTING_CHANGE_MARK_WELCOME_NOTE_ON;
        PhoneSetting->WelcomeNoteOn = pNvUiSetting->PhoneSetting.WelcomeNoteOn;

        if (TRUE == PhoneSetting->WelcomeNoteOn)
        {
            PhoneSetting->ChangeMark |= IF_SETTING_CHANGE_MARK_WELCOME_NOTE;
            Hal_Mem_Copy(PhoneSetting->WelcomeNote, pNvUiSetting->PhoneSetting.WelcomeNote, IF_SETTING_MAX_WELCOME_NOTE_LEN);
        }
        Setting_Phone_Setting_Restore_Ind(PhoneSetting);

        // free the nvm reading buffer
        Hal_Mem_Free(pNvUiSetting);

        Display_Setting_Done(ACT_ID_ANY,PHONE_TEXT_ID_SETTING_RESTORED,ACT_ID_SETTING);

        Setting_Time_Change_Ind();
    }
    else if (SETTING_ERROR == Result)
    {
        Display_Setting_Error(ACT_ID_SETTING_ENTER_SECURITY_CODE,PHONE_TEXT_ID_CODE_ERROR,ACT_ID_SETTING,TRUE);
    }
    else if (SETTING_BACK == Result)
    {
        Nav_Back(ACT_ID_SETTING_ENTER_SECURITY_CODE);
    }
} /* Display_Setting_Security_Restore_Factory_ChkSecurityCodeCnf_Cb */

/**
 * Callback of Setting_Security_Settings, phone security
 * param (in) Btn: lv_obj_t*
 * param (in) Event: lv_event_t
 * return lv_res_t: always return LV_RES_OK
 */
VOID Display_Setting_Security_Restore_Factory_Cb(lv_obj_t *Btn, lv_event_t Event)
{
    if(LV_EVENT_CLICKED == Event)
    {
        Setting_Check_Security_Req(Display_Setting_Security_Restore_Factory_ChkSecurityCodeCnf_Cb);
    }
}
